import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

// 异常处理
const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

VueRouter.onError = error => {
    console.log(error, 'error');

    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = error.message.match(pattern);
    const targetPath = VueRouter.history.pending.fullPath;

    if (isChunkLoadFailed) {
        VueRouter.replace(targetPath);
    }
};

// 入口配置
const routes = [
    {
        path: "/",
        component: (resolve) => require(["@/views/index"], resolve),
        children: [
            {
                path: "",
                name: 'Index',
                component: (resolve) => require(["@/views/manage/lead"], resolve)
            },
            // 帮组中心
            {
                path: "/help",
                name: 'help',
                component: (resolve) => require(["@/views/manage/help"], resolve)
            },
            // 消息中心
            {
                path: "/message",
                name: 'message',
                component: (resolve) => require(["@/views/manage/message/message"], resolve)
            },
            // 公告详情
            {
                path: "/notice-detail",
                name: 'noticeDetail',
                component: (resolve) => require(["@/views/manage/message/notice-detail"], resolve)
            },
            // 公告主页
            {
                path: "/notice",
                name: 'notice',
                component: (resolve) => require(["@/views/manage/message/notice"], resolve)
            },
            // 店铺概括
            {
                path: "/manage-lead",
                name: 'manageLead',
                component: (resolve) => require(["@/views/manage/lead"], resolve)
            },
            // 自营商品列表
            {
                path: "/good-list",
                name: 'goodList',
                component: (resolve) => require(["@/views/manage/market/good-list"], resolve)
            },
            // 新增编辑商品
            {
                path: "/good-edit-group",
                name: 'goodEditGroup',
                component: (resolve) => require(["@/views/manage/market/good-edit-group"], resolve)
            },
            // 淘客商品列表
            {
                path: "/good-tk-list",
                name: 'goodTkList',
                component: (resolve) => require(["@/views/manage/market/good-tk-list"], resolve)
            },
            // 自营订单列表
            {
                path: "/order-list",
                name: 'orderList',
                component: (resolve) => require(["@/views/manage/market/order-list"], resolve)
            },
            // 订单详情
            {
                path: "/order-list-detail",
                name: 'orderListDetail',
                component: (resolve) => require(["@/views/manage/market/order-list-detail"], resolve)
            },
            // 自营订单列表
            {
                path: "/order-list-tk",
                name: 'orderListTk',
                component: (resolve) => require(["@/views/manage/market/order-list-tk"], resolve)
            },
            // 售后维权
            {
                path: "/good-after-protection",
                name: 'goodAfterProtection',
                component: (resolve) => require(["@/views/manage/market/good-after-protection"], resolve)
            },
            // 纠纷单列表
            {
                path: "/dispute",
                name: 'dispute',
                component: (resolve) => require(["@/views/manage/market/dispute"], resolve)
            },
            // 纠纷单列表
            {
                path: "/dispute-detail",
                name: 'disputeDetail',
                component: (resolve) => require(["@/views/manage/market/dispute-detail"], resolve)
            },
            // 商城财务
            {
                path: "/finance",
                name: 'finance',
                component: (resolve) => require(["@/views/manage/market/finance"], resolve)
            },
            // 支付宝账户申请
            {
                path: "/account-alipay-form",
                name: 'accountAlipayForm',
                component: (resolve) => require(["@/views/manage/market/account-alipay-form"], resolve)
            },
            // 微信账户申请
            {
                path: "/account-form",
                name: 'accountForm',
                component: (resolve) => require(["@/views/manage/market/account-form"], resolve)
            },
            // 充值保证金
            {
                path: "/payment-bood",
                name: 'paymentBood',
                component: (resolve) => require(["@/views/manage/market/payment-bood"], resolve)
            },
            // 账户设置
            {
                path: "/account-setting",
                name: 'accountSetting',
                component: (resolve) => require(["@/views/manage/market/account-setting"], resolve)
            },
            // 售后设置
            {
                path: "/good-after-sale",
                name: 'goodAfterSale',
                component: (resolve) => require(["@/views/manage/market/good-after-sale"], resolve)
            },
            // 售后设置～新建地址库
            {
                path: "/good-after-edit",
                name: 'goodAfterEdit',
                component: (resolve) => require(["@/views/manage/market/good-after-edit"], resolve)
            },
            // 配送管理
            {
                path: "/goods-freight",
                name: 'goodsFreight',
                component: (resolve) => require(["@/views/manage/market/goods-freight"], resolve)
            },
            // 配送管理
            {
                path: "goods-freight-edit",
                name: 'goodsFreightEdit',
                component: (resolve) => require(["@/views/manage/market/goods-freight-edit"], resolve)
            },
            // 店铺信息
            {
                path: "/shop",
                name: 'shop',
                component: (resolve) => require(["@/views/manage/market/shop"], resolve)
            },
            // 店铺公告
            {
                path: "/shop-public",
                name: 'shopPublic',
                component: (resolve) => require(["@/views/manage/market/shop-public"], resolve)
            },
            // 退店管理
            {
                path: "/shop-close-apply",
                name: 'shopCloseApply',
                component: (resolve) => require(["@/views/manage/market/shop-close-apply"], resolve)
            },
            // 满减优惠
            {
                path: "/full-reduction",
                name: 'fullReduction',
                component: (resolve) => require(["@/views/manage/market/full-reduction"], resolve)
            },
            // 团购优惠
            {
                path: "/group-buying",
                name: 'groupBuying',
                component: (resolve) => require(["@/views/manage/market/group-buying"], resolve)
            },
        ]
    },
    {
        path: "/perfect",
        name: 'perFect',
        component: (resolve) => require(["@/views/manage/perfect"], resolve)
    },
    {
        path: "/template",
        name: 'template',
        component: (resolve) => require(["@/views/TEMPLATE"], resolve)
    }
];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

export default router;
